FallingBlock = class(Entity)

function FallingBlock:init(block,x,y,z)
    Entity.init(self)
    self.blockType = block
    self:setSize(1,1)
    self.heightOffset = 0
    self:setPos(x,y,z)
end

function FallingBlock:setPos(x,y,z)
    self.x,self.y,self.z = x,y,z
    self.aabb = AABB(x,y,z,x + self.width,y + self.height,z + self.width)
end

function FallingBlock:tick()
    self.yo = self.y
    self.yd = self.yd - 0.05
    self:move(0,self.yd,0)
    self.x,self.z = math.floor(self.x),math.floor(self.z)
    if self.verticalCollision then
        self.falling = false
        self.removed = true
        world:setBlock(self.x,math.floor(self.y),self.z,self.blockType.id)
    else
        self.yd = self.yd * 0.9
    end
    if self.y < -64 then
        self.removed = true
    end
end

function FallingBlock:render(a,t)
    local x,y,z = self.x,self.yo + (self.y - self.yo) * a,self.z
    local c1 = 255
    local c2 = 204
    local c3 = 178.5
    local c4 = 153
    t:brightb(c2 * self:getBeightness(x - 1,y,z))
    self:renderFace(t,x,y,z,Left)
    t:brightb(c2 * self:getBeightness(x + 1,y,z))
    self:renderFace(t,x,y,z,Right)
    t:brightb(c4 * self:getBeightness(x,y - 1,z))
    self:renderFace(t,x,y,z,Bottom)
    t:brightb(c1 * self:getBeightness(x,y + 1,z))
    self:renderFace(t,x,y,z,Top)
    t:brightb(c3 * self:getBeightness(x,y,z - 1))
    self:renderFace(t,x,y,z,Back)
    t:brightb(c3 * self:getBeightness(x,y,z + 1))
    self:renderFace(t,x,y,z,Front)
end

function FallingBlock:getBeightness(x,y,z)
    return (1 - GameSetting.Brightness) * world:getBrightness(x,math.floor(y),z) / Brightness.Max + GameSetting.Brightness
end

function FallingBlock:renderFace(t,x,y,z,face)
    local tex = self.blockType:getTex(face)
    local u0 = (tex % 16) / 16
    local u1 = u0 + 15.99 / 256
    local v0 = tex // 16 / 16
    local v1 = v0 + 15.99 / 256
    
    local x0 = x
    local x1 = x + 1
    local y0 = y
    local y1 = y + 1
    local z0 = z
    local z1 = z + 1
    
    if face == Front then
        t:vertexUV(x0,y0,z1,u0,v0)
        t:vertexUV(x1,y0,z1,u1,v0)
        t:vertexUV(x1,y1,z1,u1,v1)
        t:vertexUV(x1,y1,z1,u1,v1)
        t:vertexUV(x0,y1,z1,u0,v1)
        t:vertexUV(x0,y0,z1,u0,v0)
    elseif face == Back then
        t:vertexUV(x1,y0,z0,u0,v0)
        t:vertexUV(x0,y0,z0,u1,v0)
        t:vertexUV(x0,y1,z0,u1,v1)
        t:vertexUV(x0,y1,z0,u1,v1)
        t:vertexUV(x1,y1,z0,u0,v1)
        t:vertexUV(x1,y0,z0,u0,v0)
    elseif face == Top then
        t:vertexUV(x0,y1,z1,u0,v0)
        t:vertexUV(x1,y1,z1,u1,v0)
        t:vertexUV(x1,y1,z0,u1,v1)
        t:vertexUV(x1,y1,z0,u1,v1)
        t:vertexUV(x0,y1,z0,u0,v1)
        t:vertexUV(x0,y1,z1,u0,v0)
    elseif face == Bottom then
        t:vertexUV(x0,y0,z0,u0,v0)
        t:vertexUV(x1,y0,z0,u1,v0)
        t:vertexUV(x1,y0,z1,u1,v1)
        t:vertexUV(x1,y0,z1,u1,v1)
        t:vertexUV(x0,y0,z1,u0,v1)
        t:vertexUV(x0,y0,z0,u0,v0)
    elseif face == Left then
        t:vertexUV(x0,y0,z0,u0,v0)
        t:vertexUV(x0,y0,z1,u1,v0)
        t:vertexUV(x0,y1,z1,u1,v1)
        t:vertexUV(x0,y1,z1,u1,v1)
        t:vertexUV(x0,y1,z0,u0,v1)
        t:vertexUV(x0,y0,z0,u0,v0)
    else
        t:vertexUV(x1,y0,z1,u0,v0)
        t:vertexUV(x1,y0,z0,u1,v0)
        t:vertexUV(x1,y1,z0,u1,v1)
        t:vertexUV(x1,y1,z0,u1,v1)
        t:vertexUV(x1,y1,z1,u0,v1)
        t:vertexUV(x1,y0,z1,u0,v0)
    end
end